package com.youngking.guns.modular.company.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.youngking.guns.core.base.controller.BaseController;
import com.youngking.guns.core.common.exception.BizExceptionEnum;
import com.youngking.guns.core.exception.GunsException;
import com.youngking.guns.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.youngking.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.youngking.guns.modular.system.model.Company;
import com.youngking.guns.modular.company.service.ICompanyService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-08-04 16:21:05
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    private String PREFIX = "/company/company/";

    @Autowired
    private ICompanyService companyService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "company.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/company_add")
    public String companyAdd() {
        return PREFIX + "company_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/company_update/{companyId}")
    public String companyUpdate(@PathVariable Integer companyId, Model model) {
        Company company = companyService.selectById(companyId);
        model.addAttribute("item",company);
        LogObjectHolder.me().set(company);
        return PREFIX + "company_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return companyService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Company company) {
        companyService.insert(company);
        if(company.getIsdefault()==1){
            Company company1 = new Company();
            EntityWrapper ew = new EntityWrapper();
            ew.setEntity(new Company());
            ew.where("isdefault <> {0}",company.getId());
            company1.setIsdefault(0);
            companyService.update(company1,ew);
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer companyId) {
        companyService.deleteById(companyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Company company) {
        companyService.updateById(company);
        if(company.getIsdefault()==1){
            Company company1 = new Company();
            EntityWrapper ew = new EntityWrapper();
            ew.setEntity(new Company());
            ew.where("isdefault <> {0}",company.getId());
            company1.setIsdefault(0);
            companyService.update(company1,ew);
        }
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{companyId}")
    @ResponseBody
    public Object detail(@PathVariable("companyId") Integer companyId) {
        return companyService.selectById(companyId);
    }
}
